package qlsl.androiddesign.view.subview.commonview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;
import qlsl.androiddesign.activity.commonactivity.MainActivity;
import qlsl.androiddesign.activity.subactivity.BannerCategoryActivity;
import qlsl.androiddesign.activity.subactivity.GzzxActivity;
import qlsl.androiddesign.activity.subactivity.JrztActivity;
import qlsl.androiddesign.activity.subactivity.PostingCreateActivity;
import qlsl.androiddesign.activity.subactivity.PostingDetailActivity;
import qlsl.androiddesign.activity.subactivity.PostingHotActivity;
import qlsl.androiddesign.activity.subactivity.RecruitActivity;
import qlsl.androiddesign.activity.subactivity.SearchActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingListActivity;
import qlsl.androiddesign.activity.subactivity.ZlfxActivity;
import qlsl.androiddesign.activity.subactivity.TestVpVpActivity;
import qlsl.androiddesign.adapter.subadapter.ForumMenuAdapter;
import qlsl.androiddesign.adapter.subadapter.PostingAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.HomeBanner;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.entity.otherentity.ThreeCategory;
import qlsl.androiddesign.fragment.commonfragment.FragmentTab3;
import qlsl.androiddesign.http.service.commonservice.HomeService;
import qlsl.androiddesign.http.service.subservice.ForumService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshScrollView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.ChildViewPager;
import qlsl.androiddesign.view.commonview.ChildViewPager.OnSingleTouchListener;
import qlsl.androiddesign.view.indicatorview.AutoPlayManager;
import qlsl.androiddesign.view.indicatorview.ImageIndicatorView.OnItemClickListener;
import qlsl.androiddesign.view.indicatorview.IndicatorView;

public class TabView3 extends FunctionView<MainActivity> implements OnItemClickListener, OnSingleTouchListener {

	private FragmentTab3 fragment;

	private IndicatorView indicatorView;

	private AutoPlayManager autoBrocastManager;

	private PullToRefreshScrollView refreshView;

	private GridView gridView;

	private ListView listView;

	private List<Posting> list = new ArrayList<Posting>();

	private Pager pager;

	public TabView3(FragmentTab3 fragment, ViewGroup rootView, ViewGroup contentView) {
		super((MainActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	public void onTabResume() {
		refreshView.getRefreshableView().smoothScrollTo(0, 0);
		ForumService.queryNewPostingList(1, null, this, fragment);
	}

	protected void initView(View view) {
		setTitle(activity.getResources().getString(R.string.tab3));
		setRightButtonResource(R.drawable.personal_h);
		showRightButton();
		refreshView = findViewById(R.id.refreshScrollView);
		gridView = findViewById(R.id.gridView);
		listView = findViewById(R.id.listView);
		indicatorView = findViewById(R.id.net_indicate_view);
		indicatorView.setIndicateStyle(IndicatorView.INDICATE_ROUND_STYLE);
		indicatorView.setupLayoutByDefaultResource(1);
		indicatorView.show();
	}

	protected void initData() {
		setGridViewData();
		startHomeImageManager();
		HomeService.queryBannerList(this, fragment);
		ForumService.queryNewPostingList(1, null, this, fragment);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
		indicatorView.setOnItemClickListener(this);
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		viewPager.setIntercept(true);
		viewPager.setOnSingleTouchListener(this);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<Posting> list_result = (List<Posting>) map.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		} else if (t[0] instanceof List) {
			List<HomeBanner> list = (List<HomeBanner>) t[0];
			indicatorView.setTag(list);
			List<String> urls = new ArrayList<String>();
			for (HomeBanner item : list) {
				urls.add(item.getUrl());
			}
			indicatorView.setupLayout(urls);
			indicatorView.show();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.grid_item:
			doClickGridItem(view);
			break;
		case R.id.list_item:
			doClickListItem(view);
			break;
		}
	}

	private void startHomeImageManager() {
		autoBrocastManager = new AutoPlayManager(indicatorView);
		autoBrocastManager.setBroadcastEnable(true);
		autoBrocastManager.setBroadCastTimes(Integer.MAX_VALUE);
		autoBrocastManager.loop();
	}

	private void doClickGridItem(View view) {
		int position = gridView.getPositionForView(view);
		if (position == 0) {
			startActivityForResult(PostingCreateActivity.class, 0);
		} else if (position == 1) {
			startActivityForResult(JrztActivity.class, 0);
		} else if (position == 2) {
			startActivityForResult(PostingHotActivity.class, 0);
		} else if (position == 3) {
			startActivity(SearchActivity.class);
		} else if (position == 4) {
			startActivity(RecruitActivity.class);
		} else if (position == 5) {
			startActivity(TestVpVpActivity.class);// 二手车配件
		} else if (position == 6) {
			startActivityForResult(GzzxActivity.class, 0);
		} else if (position == 7) {
			startActivityForResult(ZlfxActivity.class, 0);
		}
	}

	private void doClickListItem(View view) {
		int position = listView.getPositionForView(view);
		Posting item = (Posting) listView.getAdapter().getItem(position);
		startActivity(item, PostingDetailActivity.class);
	}

	private void setGridViewData() {
		Integer[] imgs = new Integer[] { R.drawable.post_h, R.drawable.talk_h, R.drawable.hot_h, R.drawable.searcha_h,
				R.drawable.recruit_h, R.drawable.parts_h, R.drawable.consultation_h, R.drawable.share_h };
		String[] texts = new String[] { "发帖", "今日杂谈", "热帖", "搜索", "招聘求职", "二手配件", "故障咨询", "资料分享" };
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int index = 0; index < imgs.length; index++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("icon", imgs[index]);
			map.put("text", texts[index]);
			list.add(map);
		}
		ForumMenuAdapter adapter = new ForumMenuAdapter(activity, list);
		gridView.setAdapter(adapter);
	}

	private void resetList(List<Posting> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
		if (adapter == null) {
			adapter = new PostingAdapter(activity, list);
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	private OnPullRefreshListener<ScrollView> refreshListener = new OnPullRefreshListener<ScrollView>() {

		public void onPullDown() {
			ForumService.queryNewPostingList(1, null, TabView3.this, fragment);
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			ForumService.queryNewPostingList(pager.getPageNo() + 1, null, TabView3.this, fragment);
		}
	};

	@SuppressWarnings("unchecked")
	public void OnItemClick(View view, int position) {
		List<HomeBanner> list = (List<HomeBanner>) indicatorView.getTag();
		if (list != null&& list.size() > position) {
//			HomeBanner item = list.get(position);
//			startActivity(item, BannerCategoryActivity.class);
			
			ThreeCategory.RowsBean rowsBean=new ThreeCategory.RowsBean();
			rowsBean.setPhoto("");
			rowsBean.setSumcierid("0");
			rowsBean.setSumciertype("");
			startActivity(rowsBean, ShoppingListActivity.class);
		}
	}

	public void onSingleTouch() {
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		int position = viewPager.getCurrentItem();
		OnItemClick(null, position);
	}

	public void onActivityResult(int arg0, int arg1, Intent intent) {
		if (hasRefresh(intent)) {
			ForumService.queryNewPostingList(1, null, TabView3.this, fragment);
		}
	}
}
